package com.itheima.controller;

import cn.hutool.core.lang.func.Func1;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

//    @GetMapping("/list")
//    public R<List<ShoppingCart>> queryShopCartList(HttpSession session) {
//        Object userId = session.getAttribute("UserId");
//        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
//        qw.eq(ShoppingCart::getUserId, userId);
//        List<ShoppingCart> list = shoppingCartService.list(qw);
//        return list == null ? R.error("查询失败") : R.success(list);
//    }

    @GetMapping("/list")
    public R list(HttpSession session){
        // select * from shoping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long userId = (Long) session.getAttribute("UserId");
        // Long userId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        //获取用户id
        Long userId = (Long) session.getAttribute("UserId");
        //判断菜品或者套餐是否已经点过了
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        //LambdaUpdateWrapper<ShoppingCart> qw = new LambdaUpdateWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);

        if (shoppingCart.getDishId() == null) {//dishId为空说明点的是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {//dishid不为空说明点的是菜品
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }

        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one == null) {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        }else {
        one.setNumber(one.getNumber()+1);
        shoppingCartService.updateById(one);}
        return R.success(shoppingCart);
    }

    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session) {
        //菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        Long userId = (Long) session.getAttribute("UserId");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        one.setNumber(one.getNumber()-1);
        if(one.getNumber()>0){
            // >0  update更新 update set number=? where id = ?
            shoppingCartService.updateById(one);
        }else {
            // <= 0  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }


    @DeleteMapping("/clean")
    public R deleteShoppingCart(HttpSession session){
        Long userId = (Long) session.getAttribute("UserId");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        for (ShoppingCart shoppingCart : list) {

            shoppingCartService.removeById(shoppingCart.getId());
        }
        return R.success("清空购物车成功");
    }


}
